package com.spider.admin.lua;

import com.spider.admin.conf.ENVConf;
import com.spider.admin.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;

@Slf4j
@Component
public class LuaResourcesFile {

    @Autowired
    private ENVConf envConf;

    /**
     * 初始复制lua文件到代理服务器
     */
    public void copyAgentLuaFolder() {
        //复制文件夹到指定路径下
        try {
            String srcFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath()
                    + "hgg-lua";
            String targetFilePath = envConf.getUiContainerBasePath() + "_agent";
            FileUtils.copyFolder(new File(srcFilePath), new File(targetFilePath), null);

            srcFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath()
                    + "resty";
            targetFilePath = targetFilePath + "/lualib";
            FileUtils.copyFolder(new File(srcFilePath), new File(targetFilePath), null);
            log.info("完成Lua文件发布");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void copyAdminWebLuaFolder() {
        //复制文件夹到指定路径下
        try {
            String srcFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath()
                    + "hgg-lua";
            String targetFilePath = envConf.getUiContainerBasePath();
            FileUtils.copyFolder(new File(srcFilePath), new File(targetFilePath), null);

            srcFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath()
                    + "resty";
            targetFilePath = envConf.getUiContainerBasePath() + "/lualib";
            FileUtils.copyFolder(new File(srcFilePath), new File(targetFilePath), null);
            log.info("完成Lua文件发布");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
